function onProductClassLoad() {
    $.ajax({
        url:URL_HEAD_GOODS+TAG_CATEGORY+"/list/nested",
        type : "get",
        success: function(result) {
// alert(result.data.length);l/alert(result.data[0].name);
            // alert(result.data[0].childCategories.length);
            // alert(result.data[0].childCategories[0].name);
            const categoryCount = result.data.length;
            if (categoryCount > 0) {
                const builder = new StringBuilder();
                for (let i = 0; i < categoryCount; i++) {
                    const category = result.data[i];
                    addProductClassTableNewRow(category, builder);
                    const childCategoryCount = category.childCategories.length;
                    console.log(category)
                    console.log(category.parentId)
                    for (let j = 0; j < childCategoryCount; j++) {
                        const childCategory = category.childCategories[j];
                        console.log(childCategory)
                        addProductClassTableNewRow(childCategory, builder)
                    }
                }
            }
        },
        error:function(xhr, textStatus, errorThrown) {
            showError("导入分类管理失败", xhr, textStatus, errorThrown);
        }
    });
}

function addProductClassTableNewRow(category,builder) {
    builder.clear();
    builder.append("<tr><td class='first w4 c'>");
    builder.append(category.catId);
    builder.append("</td><td");
    if (category.parentId !== 0) {
        builder.append(" class='childClass'");
    }
    builder.append(">");
    builder.append(category.name);
    builder.append("</td><td class='w1 c'><a href='productClass-modify.html?name=");
    builder.append(category.name);
    builder.append("&parentId=");
    builder.append(category.parentId);
    builder.append("'>修改</a> <a href='javascript:deleteCategory(");
    builder.append(category.catId);
    builder.append(");'>删除</a></td></tr>");
    $("#tbCategoryManage tr:last").after(builder.toString());
}

function onProductClassAddLoad() {
    $(document).ready(function() {
        //alert( "onProductClassAddLoad " );
        setParentCategories(0);
    });
}

function addGoodsCategory(){
    checkAndWarnInput("#selParentId", "父类");
    checkAndWarnInput("#className", "分类名");
    const parentId = $("#selParentId").val();
    const className = $("#className").val();
    $.ajax({
        url: URL_HEAD_GOODS + TAG_CATEGORY+"/addCategory",
        //通过type来判断调用哪个方法
        type: "post",
        data: {
            "parentId": parentId,
            "name": className,
        },
        dataType: "json",
        success: function (result) {
            alert(result.msg);
            if (result.status === 0) {
                //跳转到登录页面
                $(location).attr("href", "../manae/productClass.html");
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            showError("添加异常", xhr, textStatus, errorThrown);
        }
    });
}

function setParentCategories(parentId) {
    $.ajax({
        url: URL_HEAD_GOODS + TAG_CATEGORY + "/list/parents",
        type: "get" ,
        success: function(result) {
            const categoryCount = result.data.length;
            if (categoryCount > 0) {
                const builder = new StringBuilder();
                builder.append("<option value='0'");
                if (parentId === 0){
                    builder.append("selected='selected'");
                }
                builder.append(">根栏目</option>");
                for (let i = 0; i < categoryCount; i++) {
                    builder.append("<option value='");
                    builder.append(result.data[i].catId);
                    builder.append("' ");
                    if (result.data[i].catId === parentId) {
                        builder.append("selected=\"selected\"");
                    }
                    builder.append(">");
                    builder.append(result.data[i].name);
                    builder.append("</option>");
                }
                $("#selParentId").append(builder.toString());
            }},
        error: function (xhr,textStatus, errorThrown) {
            showError("导入分类列表失败", xhr, textStatus, errorThrown);
        }
    });
}
function onProductClassModifyLoad() {
    $(document).ready(function() {
// alert("onProductClassModifyLoad");
        setParentCategories(Number(getQueryString("parentId")));
        $("#newCategoryName").val(getQueryString("name"));
    });
}

function addCategory(){
    //alert("add category");
    modifyCategory(null);
}



function updateCategory(){
    modifyCategory(getQueryString("name"));
}

function modifyCategory(oldName) {
    const newCategoryName = $("#newCategoryName").val();
    if ( !newCategoryName) {
        alert("分类名称不得为空");
        return;
    }
    const parentId = Number($("#selParentId" ).val());
    //alert(parentId);
    $.ajax({
        url: URL_HEAD_GOODS + TAG_CATEGORY + "/update",
        type: "get" ,
        headers:{
            "tokenValue": localStorage.getItem(ITEM_KEY_TOKEN_VALUE)
        },
        data: {
            "parentId" : parentId,
            "oldName" : oldName ,
            "newName" : newCategoryName
        },
        dataType : "json",
        success:function(result) {
            alert(result.msg);
            if (result.status === 0) {
                location.reload();
            } else if (result.status === 1) {
                $(location).attr("href","productClass.html");
            }
        },
        error:function(xhr, textStatus, errorThrown) {
            showError("修改分类名称异常", xhr, textStatus, errorThrown);
        }
    });
}

function onProductListLoad() {
    const parentName = getQueryString("parentName");
    const name = getQueryString("name");
    const catId = getQueryString("id");
    const builder = new StringBuilder();
    builder.append("您现在的位置:<a href=\"index.html\">易买网</a>&gt;");
    builder.append(parentName);
    builder.append( "</a> &gt;");
    builder.append(name);
    $("#position").append(builder.toString());
    setAllSameCatProductPanel(catId);
}

function deleteCategory(catId){
    // alert(typeof(catId));
    $.ajax({
        url: URL_HEAD_GOODS + TAG_CATEGORY + "/delete/" + catId,
        headers: {
            "tokenValue":localStorage.getItem(ITEM_KEY_TOKEN_VALUE)
        },
        type:"delete",
        dataType :"json",
        success:function(result) {
            alert(result.msg);
            if (result.status === O) {
                location.reload();
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            showError("网络异常导致删除失败", xhr, textstatus, errorThrown);
        }
    });
}




